//
//  LEStatisticsSevenController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/23.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEStatisticsSevenController.h"
#import "PNChart.h"
#import "MakerTools.h"

@interface LEStatisticsSevenController ()

@property (strong, nonatomic) PNLineChart *lineChart;

@property (strong, nonatomic) PNLineChart *lineChart1;

@end

@implementation LEStatisticsSevenController

- (void)setupUI
{
    [super setupUI];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"七日订单销售曲线";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.lineChart = [self getChartWith:CGRectMake(0, 0, LEScreenWidth, (LEScreenHeight - 84)/2)];
    [self.view addSubview:self.lineChart];
    [self.lineChart setYLabels:@[@"0单",@"10单", @"20单",@"30单",@"40单",@"50单",@"60单"]];
    self.lineChart.yFixedValueMax = 90.0;
    self.lineChart.yFixedValueMin = 0.0;

    
    self.lineChart1 = [self getChartWith:CGRectMake(0,(LEScreenHeight - 84)/2+10 , LEScreenWidth, (LEScreenHeight - 84)/2)];
    [self.view addSubview:self.lineChart1];
    [self.lineChart1 setYLabels:@[@"0元",@"100元", @"200元",@"300元",@"400元",@"500元",@"600元"]];
    self.lineChart1.yFixedValueMax = 700.0;
    self.lineChart1.yFixedValueMin = 0.0;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
- (void)loadData
{
    //元数据
    
    [self requestRefreshData];
    
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.start_time forKey:@"start_time"];
    [dict setObject:self.end_time forKey:@"end_time"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreStatisticsLineUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dataDict = (NSDictionary *)datas;
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
            
            //把所有的 key, 包装到可变数组中
            NSMutableArray *tempKey = [NSMutableArray arrayWithArray:dataDict.allKeys];
            NSMutableArray *orderKey = [MakerTools descend:tempKey];
            
            NSMutableArray *xLabels = [weakSelf xLabels:orderKey];
            [weakSelf.lineChart setXLabels:xLabels];
            [weakSelf.lineChart1 setXLabels:xLabels];
            
            NSMutableArray *dataSources = [weakSelf getHandleData:dataDict orderKeyArray:orderKey];
            NSMutableArray *orderLineDatas = [weakSelf getLineData:dataSources with:0];
            NSMutableArray *saleLineDatas = [weakSelf getLineData:dataSources with:1];
            
            //判断是否要重设 y 轴
            [weakSelf setYvalue:orderLineDatas withType:0];
            [weakSelf setYvalue:saleLineDatas withType:1];
            
            weakSelf.lineChart.chartData = [weakSelf setLineData:orderLineDatas];
            [weakSelf.lineChart strokeChart];
            weakSelf.lineChart1.chartData = [weakSelf setLineData:saleLineDatas];
            [weakSelf.lineChart1 strokeChart];
            
        }else if ([datas isKindOfClass:[NSNull class]])
        {
            LELog(@"%s---%d---没有数据",__func__,__LINE__);

            NSArray *notReallData = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0"];
            weakSelf.lineChart.chartData = [weakSelf setLineData:notReallData];
            [weakSelf.lineChart strokeChart];
            weakSelf.lineChart1.chartData = [weakSelf setLineData:notReallData];
            [weakSelf.lineChart1 strokeChart];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
#pragma mark- privateMethod
/**
 根据请求回来的数据设置判断是否要重置 y 轴展示
 0 是订单 1 是销售
 @param dataSource 请求回来的数据
 */
- (void)setYvalue:(NSArray *)dataSource withType:(int)type
{
    //取出最大值
    int max = [[dataSource valueForKeyPath:@"@max.intValue"] intValue];
    LELog(@"%s---%d---%d",__func__,__LINE__,max);
    if (type == 0)
    {
        //如果订单超过60,那么重新设置 Y轴.
        if (max>60)
        {
            [self setYUI:max withType:type];
        }
    }else
    {
        //如果订单金额超过600,重新设置 y轴
        if (max > 600)
        {
            [self setYUI:max withType:type];
        }
    }
}

/**
 设置 y 轴显示
 */
- (void)setYUI:(int)max withType:(int)type
{
    //63--->60 *2
    int maxPlus = (max - max%10)*2;
    //120/6
    int unitNumber = max /6;
    self.lineChart.yFixedValueMax = maxPlus;
    self.lineChart.yFixedValueMin = 0.0;
    
    //设置单位
    NSMutableArray *yValue = [NSMutableArray array];
    if (type == 0) {
        for (int i = 0; i < 7; i++)
        {
            if (i == 6) {
                [yValue addObject:[NSString stringWithFormat:@"%d单",max]];
                break;
            }
            [yValue addObject:[NSString stringWithFormat:@"%d单",(unitNumber *i)]];
          
        }
        [self.lineChart setYLabels:yValue];
    }else{
        for (int i = 0; i < 7; i++)
        {
            if (i == 6) {
                [yValue addObject:[NSString stringWithFormat:@"%d元",max]];
                break;
            }
            [yValue addObject:[NSString stringWithFormat:@"%d元",(unitNumber *i)]];
        }
        [self.lineChart1 setYLabels:yValue];
    }
    
}

/**
 给表格设置数据
 
 @param data <#data description#>
 */
- (NSArray *)setLineData:(NSArray *)data
{
    PNLineChartData *data01 = [PNLineChartData new];
    
    //线的颜色
    data01.color = PNFreshGreen;
    //线上文字颜色
    data01.pointLabelColor = [UIColor blackColor];
    //线的透明度
    data01.alpha = 0.9f;
    //是否显示线上的文字
    data01.showPointLabel = YES;
    //线上的字体
    data01.pointLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:9.0];
    //点的个数
    data01.itemCount = data.count;
    //点的颜色和形状
    data01.inflexionPointColor = PNDeepGrey;
    data01.inflexionPointStyle = PNLineChartPointStyleCircle;
    
    //给点设置数值
    data01.getData = ^(NSUInteger index) {
        CGFloat yValue = [data[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    return @[data01];
}
/**
 创建一个 LineChart

 @param frame <#frame description#>
 @return <#return value description#>
 */
- (PNLineChart *)getChartWith:(CGRect)frame
{
    PNLineChart *lineChart = [[PNLineChart alloc] initWithFrame:frame];
    
    //网格线的颜色
    lineChart.yGridLinesColor = [UIColor redColor];
    lineChart.showYGridLines = YES;
    lineChart.backgroundColor = [UIColor whiteColor];
    //显示坐标轴
    lineChart.showCoordinateAxis = YES;
    
    //设置 y 轴样式
    lineChart.yLabelColor = [UIColor blackColor];
    lineChart.yLabelFormat = @"%1.1f";
    
    //设置 x 轴样式
    lineChart.xLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:8.0];
    lineChart.xLabelColor = [UIColor blackColor];
    
    NSArray *days = [MakerTools getWeekDay];
    [lineChart setXLabels:days];

     return lineChart;
}
/**
 把 key 2017 删除掉
 
 @param keys <#keys description#>
 @return <#return value description#>
 */
- (NSMutableArray *)xLabels:(NSArray *)keys
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *key in keys) {
        [array addObject:[key substringFromIndex:5]];
    }
    return array;
}
/**
 处理成需要的数据
 
 @param dict  源数据
 @param keys 排序好的 keys 数组
 */
- (NSMutableArray *)getHandleData:(NSDictionary *)dictInfo orderKeyArray:(NSArray *)keys
{
    NSMutableArray *dataSource = [NSMutableArray array];
    for (NSString *key in keys)
    {
        id dict = dictInfo[key];
        if ([dict isKindOfClass:[NSNull class]])
        {
            NSMutableDictionary *dicts = [NSMutableDictionary dictionary];
            [dicts setValue:@"0" forKey:@"dayval"];
            [dicts setValue:@"0" forKey:@"allnum"];
            [dicts setValue:@"0" forKey:@"salesum"];
            [dicts setValue:key forKey:@"addtime"];
            [dataSource addObject:dicts];
        }else{
            [dataSource addObject:dict];
        }
        
    }
    return dataSource;
}
/**
 处理折线图需要的数据
 type = 0 是订单数据, type = 1 是销售额数据
 @param dataSource <#dataSource description#>
 */
- (NSMutableArray *)getLineData:(NSMutableArray *)dataSource with:(NSInteger)type
{
    NSMutableArray *tempArray = [NSMutableArray array];
    for (NSDictionary *dict in dataSource) {
        NSString *orderNumber = nil;
        if (type == 0) {
            orderNumber = dict[@"allnum"];
        }
        if (type == 1) {
            orderNumber = dict[@"salesum"];
        }
        
        [tempArray addObject:orderNumber];
    }
    return tempArray;
}
@end









